01 毛笔字效果

无意中在 GitHub 上找到一个毛笔字效果的项目 canvas_WritingBrush 。其效果如下图所示:

效果图

网页源码精简为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<title> canvas 递进</title>
<style type="text/css">
#canvasId {
background-color: #f2e0ba;
}
</style>
</head>
<body>
<canvas id="canvasId"></canvas><br />
<input type="button" value="clear" onclick="hw.clear();" />
<img id="pen22" src="pen22.png"/>
<img id="pen2" src="pen2.png"/>
<!-- <script type="text/javascript" src="jQuery.min.js"></script> -->
<script type="text/javascript">
var l = 20;
var arr = [];
function Handwriting(id) {
this.canvas = document.getElementById(id);
this.ctx = this.canvas.getContext("2d");
this.canvas.setAttribute('width',window.screen.width * 0.9);
this.canvas.setAttribute('height',window.screen.height *0.8);
this.ctx.fillStyle = "rgba(0,0,0,0.8)";
var _this = this;
this.canvas.addEventListener("touchstart", this.downEvent.bind(this), false);
this.canvas.addEventListener("touchmove", this.moveEvent.bind(this), false);
this.canvas.addEventListener("touchend", this.upEvent.bind(this), false);
this.canvas.addEventListener("contextmenu", function(e){ e.preventDefault() }, false);
// this.canvas.onmousedown = function (e) { _this.downEvent(e)};
// this.canvas.onmousemove = function (e) { _this.moveEvent(e)};
// this.canvas.onmouseup = function (e) { _this.upEvent(e)};
// this.canvas.onmouseout = function (e) { _this.upEvent(e)};
this.canvas.addEventListener("mousedown",function(e){ _this.downEvent(e)},false);
this.canvas.addEventListener("mousemove",function(e){ _this.moveEvent(e)},false);
this.canvas.addEventListener("mouseup",function(e){ _this.upEvent(e)},false);
this.canvas.addEventListener("mouseout",function(e){ _this.upEvent(e)},false);
this.canvas.addEventListener("click",function(e){ _this.clickEvent(e),false});
this.moveFlag = false;
this.upof = {};
this.has = [];
this.linePressure = 1;
this.smoothness = 80;
this.img = document.getElementById('pen2');
this.img1 = document.getElementById('pen22');
}
Handwriting.prototype.clear = function () {
this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
}
Handwriting.prototype.clickEvent = function(e){
this.cli = this.getXY(e);
// this.ctx.drawImage(this.img,this.cli.x - l/2,this.cli.y - l/2,l,l);
}
Handwriting.prototype.downEvent = function (e) {
this.moveFlag = true;
this.has = [];
this.upof = this.getXY(e);
// this.ctx.drawImage(this.img,(this.upof.x - this.big/2),(this.upof.y - this.big/2),this.big,this.big);
var x1 = this.upof.x;
var y1 = this.upof.y;
arr.unshift({x1,y1});
}
Handwriting.prototype.moveEvent = function (e) {
if (!this.moveFlag)
return;
e.preventDefault();
var of = this.getXY(e); //move
var up = this.upof; //down
this.has.unshift({time:new Date().getTime() ,dis:this.distance(up,of)});
var dis = 0;
var time = 0;
for (var n = 0; n < this.has.length-1; n++) {
dis += this.has[n].dis;
time += this.has[n].time-this.has[n+1].time;
if (dis>this.smoothness)
break;
}
this.upof = of;
var len = Math.round(this.has[0].dis/2)+1;
for (var i = 0; i < len; i++) {
var x = up.x + (of.x-up.x)/len*i;
var y = up.y + (of.y-up.y)/len*i;
// var r = ur + (or-ur)/len*i;
this.ctx.beginPath();
// this.ctx.arc(x,y,r,0.2*Math.PI,1.5*Math.PI,true);
// this.ctx.fill();
// var r_r = r*2;
x = x-l/2;
y = y - l/2;
arr.unshift({x,y});
this.ctx.drawImage(this.img,x,y,l,l);
l = l - 0.2;
if( l < 10) l = 10;
// console.log(l);
}
}
Handwriting.prototype.upEvent = function (e) {
this.moveFlag = false;
// console.log(p);
l = 20;
// console.log(arr);
if(arr.length >100){
for(var j = 0; j <60 ;j++){
// arr[j].x = arr[j].x - 2;
// arr[j].y = arr[j].y - 1;
arr[j].x = arr[j].x-l/4;
arr[j].y = arr[j].y - l/4;
this.ctx.drawImage(this.img,arr[j].x,arr[j].y,l,l);
l = l - 0.3;
if( l < 5) l = 5;
}
l = 20;
arr = [];
}
if (arr.length==1) {
// arr[0].x =
this.ctx.drawImage(this.img,arr[0].x1 - l/2,arr[0].y1 - l/2,l,l);
console.log(arr[0].x);
arr = [];
}
console.log(arr);
}
Handwriting.prototype.getXY = function (e)
{
var x = e.clientX || e.touches[0].clientX;
var y = e.clientY || e.touches[0].clientY;
// // return {
// x : e.clientX - this.canvas.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft),
// y : e.clientY - this.canvas.offsetTop + (document.body.scrollTop || document.documentElement.scrollTop)
// }
return {
x : x - this.canvas.offsetLeft + (document.body.scrollLeft || document.documentElement.scrollLeft),
y : y - this.canvas.offsetTop + (document.body.scrollTop || document.documentElement.scrollTop)
}
}
Handwriting.prototype.distance = function (a,b)
{
var x = b.x-a.x , y = b.y-a.y;
return Math.sqrt(x*x+y*y);
}
// $('input').eq(1).val(5);
// $('input').eq(2).val(20);
var hw = new Handwriting("canvasId");
hw.linePressure = 2.5;
hw.smoothness = 100;
</script>
</body>
</html>

本人参考它的源码并在 Android 上实现类似的自定义控件,其效果如下:

Android 效果图

源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.xxt.xian.view.autograph;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
import com.xxt.xian.R;
import java.util.LinkedList;
public class XAutographView extends View {
private Paint mPaint;
private boolean isTouching;
private Bitmap mPenBmp;
private RectF mRect;
private float smoothness = 80;
private float maxL = 50;
private float l;
private Bitmap mBitmap;
private Canvas mCanvas;
LinkedList<FPoint> mPoints;
LinkedList<Float> mDisList;
FPoint prePoint, tempPoint, curPoint;
public XAutographView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mRect = new RectF();
mPenBmp = BitmapFactory.decodeResource(getResources(), R.drawable.pen);
isTouching = false;
mPoints = new LinkedList<>();
mDisList = new LinkedList<>();
prePoint = new FPoint();
tempPoint = new FPoint();
curPoint = new FPoint();
mCanvas = new Canvas();
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
if (mBitmap != null) mBitmap.recycle();
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);
maxL = w / 30f;
l = maxL;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isTouching = true;
mPoints.clear();
prePoint.set(x, y);
mPoints.addFirst(new FPoint(x, y));
return true;
case MotionEvent.ACTION_MOVE:
drawMoveCanvas(x, y);
break;
case MotionEvent.ACTION_UP:
drawUpCanvas();
break;
default:break;
}
return super.onTouchEvent(event);
}
private void drawUpCanvas() {
isTouching = false;
l = maxL;
if (mPoints.size() > 200) {
for (int j = 0; j < 100; j++) {
FPoint p = mPoints.get(j);
p.x = p.x - l / 4;
p.y = p.y - l / 4;
mRect.set(p.x, p.y, p.x+l, p.y+l);
mCanvas.drawBitmap(mPenBmp, null, mRect, mPaint);
l = l - 0.3f;
// if( l < maxL / 10) l = maxL / 10;
}
mPoints.clear();
}
if (mPoints.size() == 1) {
FPoint p = mPoints.get(0);
float x = p.x - l / 2;
float y = p.y - l / 2;
mRect.set(x, y, x+l, y+l);
mCanvas.drawBitmap(mPenBmp, null, mRect, mPaint);
mPoints.clear();
}
}
private void drawMoveCanvas(final float x, final float y) {
new Thread(new Runnable() {
@Override
public void run() {
if (isTouching) {
curPoint.set(x, y);
tempPoint.set(prePoint.x, prePoint.y);
mDisList.addFirst(getDistance(tempPoint, curPoint));
float dis = 0;
for (int n = 0; n < mDisList.size(); n++) {
dis += mDisList.get(n);
if (dis > smoothness) break;
}
prePoint.set(curPoint.x, curPoint.y);
int len = Math.round(mDisList.get(0) / 2) + 1;
for (int i = 0; i < len; i++) {
float x1 = tempPoint.x + (curPoint.x - tempPoint.x) / len * i;
float y1 = tempPoint.y + (curPoint.y - tempPoint.y) / len * i;
x1 = x1 - l/2;
y1 = y1 - l/2;
mPoints.addFirst(new FPoint(x1, y1));
mRect.set(x1, y1, x1+l, y1+l);
mCanvas.drawBitmap(mPenBmp, null, mRect, mPaint);
l = l - 0.2f;
if( l < maxL / 2) l = maxL / 2;
}
postInvalidate();
}
}
}).start();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
}
private float getDistance(FPoint p1, FPoint p2) {
float x = p2.x - p1.x;
float y = p2.y - p1.y;
return (float) Math.sqrt(x * x + y * y);
}
class FPoint {
float x;
float y;
FPoint() {}
FPoint(float x, float y) {
this.x = x;
this.y = y;
}
void set(float x, float y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FPoint point = (FPoint) o;
if (x != point.x) return false;
if (y != point.y) return false;
return true;
}
@Override
public String toString() {
return "Point(" + x + ", " + y + ")";
}
}
}package xian.xiao.tao.ui;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import androidx.annotation.Nullable;
import java.util.LinkedList;
import xian.xiao.tao.R;
public class XAutographView extends View {
private Paint mPaint;
private boolean isTouching;
private Bitmap mPenBmp;
private RectF mRect;
private float smoothness = 80;
private float maxL = 50;
private float l;
private Bitmap mBitmap;
private Canvas mCanvas;
LinkedList<XPoint> mPoints;
LinkedList<Float> mDisList;
XPoint prePoint, tempPoint, curPoint;
public XAutographView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mRect = new RectF();
mPenBmp = BitmapFactory.decodeResource(getResources(), R.mipmap.ui_x_autograph_view_nib);
isTouching = false;
mPoints = new LinkedList<>();
mDisList = new LinkedList<>();
prePoint = new XPoint();
tempPoint = new XPoint();
curPoint = new XPoint();
mCanvas = new Canvas();
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
if (mBitmap != null) mBitmap.recycle();
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas.setBitmap(mBitmap);
maxL = w / 30f;
l = maxL;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isTouching = true;
mPoints.clear();
prePoint.set(x, y);
mPoints.addFirst(new XPoint(x, y));
return true;
case MotionEvent.ACTION_MOVE:
drawMoveCanvas(x, y);
break;
case MotionEvent.ACTION_UP:
drawUpCanvas();
break;
default:break;
}
return super.onTouchEvent(event);
}
private void drawMoveCanvas(final float x, final float y) {
new Thread(new Runnable() {
@Override
public void run() {
if (isTouching) {
curPoint.set(x, y);
tempPoint.set(prePoint.x, prePoint.y);
mDisList.addFirst(getDistance(tempPoint, curPoint));
float dis = 0;
for (int n = 0; n < mDisList.size(); n++) {
dis += mDisList.get(n);
if (dis > smoothness) break;
}
prePoint.set(curPoint.x, curPoint.y);
int len = Math.round(mDisList.get(0) / 2) + 1;
for (int i = 0; i < len; i++) {
float x1 = tempPoint.x + (curPoint.x - tempPoint.x) / len * i;
float y1 = tempPoint.y + (curPoint.y - tempPoint.y) / len * i;
x1 = x1 - l/2;
y1 = y1 - l/2;
mPoints.addFirst(new XPoint(x1, y1));
mRect.set(x1, y1, x1+l, y1+l);
mCanvas.drawBitmap(mPenBmp, null, mRect, mPaint);
l = l - 0.2f;
if( l < maxL / 2) l = maxL / 2;
}
postInvalidate();
}
}
}).start();
}
private void drawUpCanvas() {
isTouching = false;
l = maxL;
if (mPoints.size() > 200) {
for (int j = 0; j < 100; j++) {
XPoint p = mPoints.get(j);
p.x = p.x - l / 4;
p.y = p.y - l / 4;
mRect.set(p.x, p.y, p.x+l, p.y+l);
mCanvas.drawBitmap(mPenBmp, null, mRect, mPaint);
l = l - 0.3f;
}
mPoints.clear();
}
if (mPoints.size() == 1) {
XPoint p = mPoints.get(0);
float x = p.x - l / 2;
float y = p.y - l / 2;
mRect.set(x, y, x+l, y+l);
mCanvas.drawBitmap(mPenBmp, null, mRect, mPaint);
mPoints.clear();
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
}
private float getDistance(XPoint p1, XPoint p2) {
float x = p2.x - p1.x;
float y = p2.y - p1.y;
return (float) Math.sqrt(x * x + y * y);
}
class XPoint {
float x;
float y;
XPoint() {}
XPoint(float x, float y) {
this.x = x;
this.y = y;
}
void set(float x, float y) {
this.x = x;
this.y = y;
}
}
}

资源图片:

pen2.png

pen22.png